﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class RoyalItemRepayDAL : IRoyalItemRepayDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public RoyalItemRepayDAL()
        {
        }
        public IList<RoyalItemRepayInfo> ListRoyalItemRepays(RoyalItemRepayInfo riInfo)
        {
            string strSQL;

            List<RoyalItemRepayInfo> infoList = new List<RoyalItemRepayInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@Seq", riInfo.Sequence);

                strSQL = "SELECT REPAY_SEQ, REF_NO, REPAY_DESCR, START_DATE, END_DATE, REMARKS"
                        + ", RECORD_STATUS, CREATED_BY, UPDATED_BY"
                    + " FROM HRM_PS_ROYAL_ITEM_REPAYS "
                    + " WHERE RECORD_STATUS not like @DeleteRecord"
                    + (riInfo.Sequence > 0 ? " AND REPAY_SEQ = @Seq" : "")
                    + " ORDER BY REF_NO";

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    RoyalItemRepayInfo Info = new RoyalItemRepayInfo(rdr.GetInt32(0)
                            , rdr.IsDBNull(1) ? "" : rdr.GetString(1)
                            , rdr.IsDBNull(2) ? "" : rdr.GetString(2)
                            , rdr.IsDBNull(3) ? (DateTime?)null : rdr.GetDateTime(3)
                            , rdr.IsDBNull(4) ? (DateTime?)null : rdr.GetDateTime(4)
                            , rdr.IsDBNull(5) ? "" : rdr.GetString(5)
                            , rdr.IsDBNull(6) ? "" : rdr.GetString(6)
                            , rdr.IsDBNull(7) ? null : (Nullable<int>)rdr.GetInt32(7)
                            , rdr.IsDBNull(8) ? null : (Nullable<int>)rdr.GetInt32(8));
                    infoList.Add(Info);
                }

                return infoList;
            }
            catch
            {
                return null;
                throw;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }        

        public void Insert(RoyalItemRepayInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(8);

                dbManager.AddParameters(0, "@RefNo", Info.RefNo);
                dbManager.AddParameters(1, "@RepayDescr", Info.Description);
                dbManager.AddParameters(2, "@StartDate", Info.StartDate);
                dbManager.AddParameters(3, "@EndDate", Info.EndDate);
                dbManager.AddParameters(4, "@Remarks", Info.Remarks);
                dbManager.AddParameters(5, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(6, "@CreatedBy", Info.CreatedBy);
                dbManager.AddParameters(7, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO HRM_PS_ROYAL_ITEM_REPAYS(REF_NO, REPAY_DESCR, START_DATE"
                        + ", END_DATE, REMARKS, RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES(@RefNo, @RepayDescr, @StartDate, @EndDate, @Remarks, @RecordStatus, @CreatedBy, @CreatedWhen)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(RoyalItemRepayInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(8);

                dbManager.AddParameters(0, "@RepaySeq", Info.Sequence);
                dbManager.AddParameters(1, "@RefNo", Info.RefNo);
                dbManager.AddParameters(2, "@RepayDescr", Info.Description);
                dbManager.AddParameters(3, "@StartDate", Info.StartDate);
                dbManager.AddParameters(4, "@EndDate", Info.EndDate);
                dbManager.AddParameters(5, "@Remarks", Info.Remarks);
                dbManager.AddParameters(6, "@UpdatedBy", Info.UpdatedBy);
                dbManager.AddParameters(7, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_PS_ROYAL_ITEM_REPAYS"
                    + " SET REF_NO = @RefNo"
                        + ", REPAY_DESCR = @RepayDescr"
                        + ", START_DATE = @StartDate"
                        + ", END_DATE = @EndDate"
                        + ", REMARKS = @Remarks"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdatedWhen"
                    + " WHERE REPAY_SEQ = @RepaySeq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(int Sequence, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@RepaySeq", Sequence);
                dbManager.AddParameters(1, "@RecordStatus", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@UpdatedBy", UpdatedBy);
                dbManager.AddParameters(3, "@UpdateWhen", DateTime.Today);

                strSQL = "UPDATE HRM_PS_ROYAL_ITEM_REPAYS"
                    + " SET RECORD_STATUS = @RecordStatus"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdateWhen"
                    + " WHERE REPAY_SEQ = @RepaySeq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
